<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12" style='border-bottom: 1px solid #d9d9d9;margin-bottom: 10px;'>
            <a-form-model-item label="所属科目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subjectId">
              <!--              <a-input v-model="model.examinationId" placeholder="请选择所属科目id"  ></a-input>-->
              <a-select v-model="model.subjectId" placeholder="请选择所属科目"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in subjectList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" style='border-bottom: 1px solid #d9d9d9;margin-bottom: 10px;'>
            <a-form-model-item label="所属考试" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="examinationId">
              <!--              <a-input v-model="model.examinationId" placeholder="请输入所属考试id"  ></a-input>-->
              <a-select v-model="model.examinationId" placeholder="请选择所属考试"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in achexaminationList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="试卷编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="examinationPaperCode">
              <a-input v-model="model.examinationPaperCode" placeholder="请输入试卷编码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="试卷名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="examinationPaperName">
              <a-input v-model="model.examinationPaperName" placeholder="请输入试卷名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="试卷类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="examinationPaperTtype">
              <j-dict-select-tag type="list" v-model="model.examinationPaperTtype"
                                 dictCode="examination_paper_ttype" placeholder="请选择试卷类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="试卷来源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="examinationPaperSource">
              <a-input v-model="model.examinationPaperSource" placeholder="请输入试卷来源"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="满分" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="examinationPaperFullMarks">
              <a-input-number v-model="model.examinationPaperFullMarks" placeholder="请输入满分" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="及格分" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="examinationPaperPassMarks">
              <a-input-number v-model="model.examinationPaperPassMarks" placeholder="请输入及格分" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="考试时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="examinationPaperTestTime">
              <j-date placeholder="请选择考试时间" v-model="model.examinationPaperTestTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="考试时长(分钟)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="examinationPaperDuration">
              <a-input-number v-model="model.examinationPaperDuration" placeholder="请输入考试时长(分钟)" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" style='border-top: 1px solid #d9d9d9;padding-top: 20px;'>
            <a-form-model-item label="所属部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sysOrgCode">
              <!--              <a-input v-model="model.sysOrgCode" placeholder="请输入所属部门"  ></a-input>-->
              <a-select v-model="model.sysOrgCode" placeholder="请选择所属部门"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in departDataList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" style='border-top: 1px solid #d9d9d9;padding-top: 20px;'>
            <a-form-model-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="izOpen">
              <j-dict-select-tag type="list" v-model="model.izOpen" disabled
                                 dictCode="iz_open" placeholder="请选择是否启用" />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="12">-->
<!--            <a-form-model-item label="是否删除" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="delFlag">-->
<!--              <j-dict-select-tag type="list" v-model="model.delFlag"-->
<!--                                 dictCode="del_flag" placeholder="请选择是否删除" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="创建人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createBy">-->
<!--              <a-input v-model="model.createBy" placeholder="请输入创建人"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="修改人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="updateBy">-->
<!--              <a-input v-model="model.updateBy" placeholder="请输入修改人"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { getDepartDataList } from '@api/commonAPI'
  import { getAchExaminationAll, getSubjectList } from '@api/achievementCommonAPI'

  export default {
    name: 'AchExaminationPaperForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/achexaminationpaper/achExaminationPaper/add",
          edit: "/achexaminationpaper/achExaminationPaper/edit",
          queryById: "/achexaminationpaper/achExaminationPaper/queryById"
        },
        departDataList: [],
        achexaminationList: [],
        subjectList: [],
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.getDepartData();
      this.getAchExaminationAllData();
      this.getSubjectData();
    },
    methods: {
      add () {
        this.modelDefault.izOpen = 1//新增默认启用
        this.modelDefault.delFlag = 0//新增默认0未删除
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      async getDepartData() {
        let res = await getDepartDataList()
        this.departDataList = []
        res.result.forEach(item => {
          this.departDataList.push({
            label: item.departName,
            value: item.orgCode
          })
        })
      },
      async getAchExaminationAllData() {
        let res = await getAchExaminationAll()
        this.achexaminationList = []
        res.result.records.forEach(item => {
          this.achexaminationList.push({
            label: item.examinationName,
            value: item.id
          })
        })
      },
      async getSubjectData() {
        let res = await getSubjectList()
        this.subjectList = []
        res.result.records.forEach(item => {
          this.subjectList.push({
            label: item.subjectName,
            value: item.id
          })
        })
      },

    }
  }
</script>
